﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TruongVu.Data;

namespace TruongVu.Business
{
    public class CustomerModel
    {
        public List<Customer> GetAll()
        {
            return (from customer in Singleton.DataContext.Customers
                    select customer).ToList();
        }

        public Customer Get(int id)
        {
            return (from customer in Singleton.DataContext.Customers
                    where customer.ID == id
                    select customer).SingleOrDefault();
        }

        public void Insert(Customer customer)
        {
            Singleton.DataContext.Customers.InsertOnSubmit(customer);
            Singleton.DataContext.SubmitChanges();
        }

        public void Update(Customer updatedCustomer)
        {
            Customer customer = (from c in Singleton.DataContext.Customers
                                 where c.ID == updatedCustomer.ID
                                 select c).SingleOrDefault();
            if (customer != null)
            {
                customer.Address = updatedCustomer.Address;
                customer.Name = updatedCustomer.Name;
                customer.Phone = updatedCustomer.Phone;
                customer.Website = updatedCustomer.Website;
                Singleton.DataContext.SubmitChanges();
            }
        }

        public void Delete(int id)
        {
            Customer customer = (from c in Singleton.DataContext.Customers
                                 where c.ID == id
                                 select c).SingleOrDefault();
            if (customer != null)
            {
                Singleton.DataContext.Customers.DeleteOnSubmit(customer);
                Singleton.DataContext.SubmitChanges();
            }
        }
    }
}
